part of _;

extension on SearchPage {
  void controller_search() async {
    var content = textController.text;
    if (content.isEmpty) {
      yToast("内容不能为空");
      return;
    }
    _insertHistory(content);
    AppUtils.goIntent(yPage, Enum_IntentType.GoProductList_content, params: {
      "content": content,
    });
  }

  void _insertHistory(String content) async {
    var result = await yRead(IFinal.Saves.historySearchs);
    List<String> historySearchs = (result == null ? [] : (json.decode(result) as List<dynamic>).map((e) => e.toString()).toList());
    if (historySearchs.contains(content)) historySearchs.remove(content);
    if (historySearchs.length >= IFinal.Configs_Server.historyMaxCount) historySearchs.removeLast();
    historySearchs.insert(0, content);
    ySave(IFinal.Saves.historySearchs, yToJson(historySearchs));
  }

  void controller_clearHistory() {
    yShowBuilder("确认删除全部历史记录？", () {
      setState(() => models.clear());
      yDelete(IFinal.Saves.historySearchs);
      yToast("清除成功");
    }, null, true);
  }
}
